<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="请输入手机号"
					:focus="isFocus"
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					maxlength="11"
					placeholder="密码"
				></wInput>
			</view>
			<wButton 
				class="wbutton"
				text="登 录"
				:rotate="isRotate" 
				@click="startLogin"
			></wButton>
			
			<!-- 其他登录 -->
			<view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-weibo" @tap="login_weibo"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-github" @tap="login_github"></view>
				</view>
			</view>
			
			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	var _this;
	import wInput from '../../../components/watch-login/watch-input.vue' //input
	import wButton from '../../../components/watch-login/watch-button.vue' //button
	
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				phoneData:'', //电话
				passData:'', //密码
				isRotate: false, //是否加载旋转
				isFocus: true ,// 是否聚焦
				appUrl:{
					appLoginUrl:'/stu/stu/appLogin'
				}
			};
		},
		onLoad: function (option) {
			this.phoneData=option.phoneData
			this.passData=option.passData
		},
		components:{
			wInput,
			wButton,
		},
		mounted() {
			_this= this;
			this.isLogin();
		},
		methods: {
			isLogin(){
				//判断缓存中是否登录过，直接登录
				try {
					const user = JSON.parse(uni.getStorageSync('user'));
					const stu = JSON.parse(uni.getStorageSync('stu'));
					if (user!="{}") {
						uni.reLaunch({
							url: '../../index/index',
						});
					}
				} catch (e) {
					 console.log(e)
				}
			},
		    startLogin(e){
                let reg = /^[1][3,4,5,7,8,9][0-9]{9}$/
				//登录
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (!reg.test(this.phoneData)) {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号格式不正确'
				    });
				    return;
				}
		        if (this.passData.length < 5) {
		            uni.showToast({
		                icon: 'none',
						position: 'bottom',
		                title: '密码不正确'
		            });
		            return;
		        }
				/* 加载中 */
				_this.isRotate=true
				setTimeout(function(){
					_this.isRotate=false
				},1000)
				uni.showLoading({
					title: '登录中'
				});
				/* 发送请求验证账号密码*/
				this.$axios.get(this.appUrl.appLoginUrl+"?phoneData="+this.phoneData+"&passData="+this.passData).then((res)=>{
					if(res.data.result==null){
						uni.showToast({
							icon: 'error',
							position: 'bottom',
							title: '账号或密码错误'
						});
					}else{
						if(res.data.message=='用户登陆'){
							uni.setStorageSync('user',res.data.result)
						}
						if(res.data.message=='学生登陆'){
							uni.setStorageSync('user',res.data.result[0])
							uni.setStorageSync('stu',res.data.result[1])
						}
						uni.showToast({
							icon: 'success',
							position: 'bottom',
							title: '登录成功'
						});
						uni.reLaunch({
							url: '/pages/index/index',
						});
					}
				})
				uni.hideLoading();
		    },
			login_weixin() {
				//微信登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_weibo() {
				//微博登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_github() {
				//github登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			}
		}
	}
</script>

<style>
	@import url("../../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
